﻿using DocumentFormat.OpenXml.Drawing.Charts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xiaoxiong.ScadaProModbusRtu.Common;

namespace xiaoxiong.ScadaProModbusRtu.childFrm
{
    public partial class FrmParamSel : Form
    {
        public FrmParamSel(List<string> selectParam)
        {
            InitializeComponent();

            if (CommonMethods.plcDevice != null)
            {
                UnselectParamList = CommonMethods.plcDevice.StoreVarList.Select(c => c.Comments).ToList();


                foreach (var item in selectParam)
                {
                    selectParamList.Add(item);
                    UnselectParamList.Remove(item);
                }

                UpdateListBox();
            }

        }

        //未选择
        private List<string> UnselectParamList = new List<string>();
        //已选择
        public List<string> selectParamList = new List<string>();

        private void UpdateListBox()
        {
            this.lst_Select.Items.Clear();
            this.lst_Select.Items.AddRange(selectParamList.ToArray());

            this.lst_unSelectList.Items.Clear();
            this.lst_unSelectList.Items.AddRange(UnselectParamList.ToArray());
        }

        // 添加单个参数
        private void btn_Add_Click(object sender, EventArgs e)
        {
            // 修复判断逻辑：使用 SelectedIndices.Count 代替 SelectedItems == null
            if (UnselectParamList.Count == 0 || lst_unSelectList.SelectedIndices.Count == 0)
                return;

            // 获取选中项（支持多选）
            var selectedItems = lst_unSelectList.SelectedItems.Cast<string>().ToList();

            foreach (var item in selectedItems)
            {
                UnselectParamList.Remove(item);
                selectParamList.Add(item);
            }

            UpdateListBox();

            // 设置选中状态：选中第一个剩余项或最后一个有效位置
            if (lst_unSelectList.Items.Count > 0)
            {
                int newIndex = Math.Min(lst_unSelectList.SelectedIndex, lst_unSelectList.Items.Count - 1);
                lst_unSelectList.SelectedIndex = newIndex >= 0 ? newIndex : 0;
            }
        }

        // 添加全部参数
        private void btn_AddAll_Click(object sender, EventArgs e)
        {
            if (UnselectParamList.Count == 0)
                return;

            // 修复集合修改冲突：先复制再清空
            var tempList = new List<string>(UnselectParamList);
            selectParamList.AddRange(tempList);
            UnselectParamList.Clear();

            UpdateListBox();

            // 设置选中到已选列表首项
            if (lst_Select.Items.Count > 0)
                lst_Select.SelectedIndex = 0;
        }

        // 移除单个参数
        private void btn_Remove_Click(object sender, EventArgs e)
        {
            // 修复判断逻辑
            if (selectParamList.Count == 0 || lst_Select.SelectedIndices.Count == 0)
                return;

            // 获取选中项（支持多选）
            var selectedItems = lst_Select.SelectedItems.Cast<string>().ToList();

            foreach (var item in selectedItems)
            {
                selectParamList.Remove(item);
                UnselectParamList.Add(item);
            }

            UpdateListBox();

            // 设置选中状态
            if (lst_Select.Items.Count > 0)
            {
                int newIndex = Math.Min(lst_Select.SelectedIndex, lst_Select.Items.Count - 1);
                lst_Select.SelectedIndex = newIndex >= 0 ? newIndex : 0;
            }
        }

        // 移除全部参数
        private void btn_RemoveAll_Click(object sender, EventArgs e)
        {
            if (selectParamList.Count == 0)
                return;

            // 修复集合操作方式
            var tempList = new List<string>(selectParamList);
            UnselectParamList.AddRange(tempList);
            selectParamList.Clear();

            UpdateListBox();

            // 设置选中到未选列表首项
            if (lst_unSelectList.Items.Count > 0)
                lst_unSelectList.SelectedIndex = 0;
        }

        private void btn_Select_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
